package com.itheima.employeesys.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import com.itheima.employeesys.bean.User;

/**
 * 用户注册对话框类
 * 用于处理用户注册功能，使用User对象和ArrayList存储用户数据
 */
public class RegisterDialog extends JDialog {
    private JTextField usernameField;
    private JTextField loginNameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JButton registerButton;
    private JButton cancelButton;

    // 用户集合引用
    private List<User> users;

    /**
     * 构造函数
     * @param parent 父窗口
     * @param users 用户集合
     */
    public RegisterDialog(Frame parent, List<User> users) {
        super(parent, "用户注册", true);
        this.users = users;
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        setFrameProperties();
    }

    /**
     * 初始化组件
     */
    private void initializeComponents() {
        // 用户名字段
        usernameField = new JTextField(20);
        usernameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 登录名字段
        loginNameField = new JTextField(20);
        loginNameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 密码字段
        passwordField = new JPasswordField(20);
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 确认密码字段
        confirmPasswordField = new JPasswordField(20);
        confirmPasswordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 按钮
        registerButton = new JButton("注册");
        registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        registerButton.setBackground(new Color(70, 130, 180));
        registerButton.setForeground(Color.WHITE);
        registerButton.setFocusPainted(false);
        registerButton.setBorderPainted(false);

        cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cancelButton.setBackground(new Color(211, 211, 211));
        cancelButton.setFocusPainted(false);
        cancelButton.setBorderPainted(false);
    }

    /**
     * 设置布局
     */
    private void setupLayout() {
        setLayout(new BorderLayout());

        // 创建表单面板
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBackground(Color.WHITE);
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        
        // 设置标签靠左对齐
        gbc.anchor = GridBagConstraints.WEST;

        // 用户名行
        gbc.gridx = 0; gbc.gridy = 0;
        formPanel.add(new JLabel("用户名:"), gbc);
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL; // 使输入框水平填充
        gbc.weightx = 1.0; // 设置水平权重
        formPanel.add(usernameField, gbc);

        // 登录名行
        gbc.gridx = 0; gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE; // 重置填充
        gbc.weightx = 0.0; // 重置权重
        formPanel.add(new JLabel("登录名:"), gbc);
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL; // 使输入框水平填充
        gbc.weightx = 1.0; // 设置水平权重
        formPanel.add(loginNameField, gbc);

        // 密码行
        gbc.gridx = 0; gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE; // 重置填充
        gbc.weightx = 0.0; // 重置权重
        formPanel.add(new JLabel("密码:"), gbc);
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL; // 使输入框水平填充
        gbc.weightx = 1.0; // 设置水平权重
        formPanel.add(passwordField, gbc);

        // 确认密码行
        gbc.gridx = 0; gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE; // 重置填充
        gbc.weightx = 0.0; // 重置权重
        formPanel.add(new JLabel("确认密码:"), gbc);
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL; // 使输入框水平填充
        gbc.weightx = 1.0; // 设置水平权重
        formPanel.add(confirmPasswordField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0)); 
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 7, 0)); // 底部边距7像素，使按钮向上移动
        buttonPanel.add(registerButton);
        buttonPanel.add(cancelButton);

        // 添加到主面板
        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }


    /**
     * 设置事件处理器
     */
    private void setupEventHandlers() {
        // 注册按钮事件
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performRegister();
            }
        });

        // 取消按钮事件
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    /**
     * 执行注册操作
     */
    private void performRegister() {
        String username = usernameField.getText().trim();
        String loginName = loginNameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());

        // 验证输入
        if (username.isEmpty() || loginName.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 检查密码确认
        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "密码和确认密码不匹配！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 检查用户名或登录名是否已被使用
        boolean userExists = false;
        for (User existingUser : users) {
            // 检查新用户名是否与现有用户的用户名或登录名冲突
            // 检查新登录名是否与现有用户的用户名或登录名冲突
            if (existingUser.getUsername().equals(username) || 
                existingUser.getLoginName().equals(username) ||
                existingUser.getUsername().equals(loginName) || 
                existingUser.getLoginName().equals(loginName)) {
                userExists = true;
                break;
            }
        }

        if (userExists) {
            JOptionPane.showMessageDialog(this, "用户名或登录名已被使用，请选择其他名称！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 注册成功，创建新用户并添加到集合
        User newUser = new User(username, password, loginName);
        users.add(newUser);
        JOptionPane.showMessageDialog(this, "注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
        dispose();
    }

    /**
     * 设置窗口属性
     */
    private void setFrameProperties() {
        setSize(350, 250);
        setLocationRelativeTo(getParent());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
    }
}
